home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / editor / auror300.zip / FINDO.AML < prev    next >
Text File  |  1996-07-17  |  8KB  |  314 lines

  1. //--------------------------------------------------------------------
  2. // FINDO.AML
  3. // Find Occurrences, (C) 1993-1996 by nuText Systems
  4. //
  5. // (See Findo.dox for user help)
  6. //
  7. // This macro searches the current edit window for all occurrences of a
  8. // search string or regular expression pattern.
  9. //
  10. // The search string and search options can be passed to this macro, (as
  11. // the multistring: search/options). If nothing is passed, the user will
  12. // be prompted to enter the search string and options.
  13. //
  14. // Usage:
  15. //
  16. // Select this macro from the Macro List (on the Macro menu), or run it
  17. // from the macro picklist <shift f12>.
  18. //
  19. // This macro can also be run by selecting Find Occurrences from the
  20. // Search menu.
  21. //--------------------------------------------------------------------
  22.  
  23. // compile time macros and function definitions
  24. include bootpath "define.aml"
  25.  
  26. // colors
  27. constant findo_border_color        = color black on gray
  28. constant findo_title_color         = color black on gray
  29. constant findo_text_color          = color black on gray
  30. constant findo_border_flash_color  = color brightgreen on gray
  31. constant findo_cursor_color        = color black on brightgreen
  32. constant findo_menu_color          = color white on blue
  33. constant findo_menu_hotkey_color   = color yellow on blue
  34. constant findo_menu_hilite_color   = color white on cyan
  35.  
  36. // test for edit windows
  37. if not wintype? "edit" then
  38.   msgbox "Edit windows only!"
  39.   return
  40. end
  41.  
  42. forward gotoline
  43. forward editlines
  44.  
  45. variable search_string, options, o
  46.  
  47. // edit fields/groupbox windows
  48. variable f1, g1, g2
  49.  
  50. // retrieve history in dlgbox edit fields and options
  51. function updatehist
  52.   variable searchstr, replstr, options
  53.   n = splitstr '' (gettext) ref searchstr ref replstr ref options
  54.   if n == 2 then
  55.     options = replstr
  56.   end
  57.  
  58.   // search string
  59.   buffer = getwinbuf f1
  60.   delchar MAX_COL 1 '' buffer
  61.   instext searchstr 1 '' buffer
  62.  
  63.   // default options
  64.   if not options and n <= 1 then
  65.     options = _SearchOpt
  66.   end
  67.  
  68.   setgroupbox options "iwx" g1
  69.   setgroupbox options "bs"  g2
  70.   col getlinelen + 1
  71. end
  72.  
  73.  
  74. // display find dialog box
  75. function findodlg (s title)
  76.   variable searchstr, options1, options2
  77.   dialog "Find Occurrences" 66 7 'cp'
  78.   f1 = field "&Search for: >" 3 2  38 s "_find" whenselect "updatehist"
  79.   // group box 1
  80.   g1 = groupbox '' 3 4
  81.     (menu ''
  82.        item " [ ] &Ignore Case"
  83.        item " [ ] &Whole Words"
  84.        item " [ ] Regular E&Xpression "
  85.      end ) '' _SearchOpt 'iwx'
  86.   // group box 2
  87.   g2 = groupbox '' 29 4
  88.     (menu ''
  89.        item " [ ] Marked &Block Only  "
  90.        item " [ ] Skip &Folds"
  91.      end) '' _SearchOpt 'bs'
  92.   button "O&k"    56 2 8
  93.   button "Cancel" 56 4 8
  94.   if (getdialog ref searchstr ref options1 ref options2) == 'Ok' then
  95.     joinstr '' searchstr options1 + options2
  96.   end
  97. end
  98.  
  99. // prompt for a search string if none specified
  100. string_and_opt = arg 3
  101. if not string_and_opt then
  102.   string_and_opt = if _PromptStyle == 'd' then
  103.                      findodlg
  104.                    else
  105.                      ask "[string/biswx] Find occurrences of"  "_find"
  106.                    end
  107.   if not string_and_opt then
  108.     return
  109.   end
  110.   addhistory "_find" string_and_opt
  111. end
  112.  
  113. n = splitstr '' string_and_opt ref search_string ref options ref o
  114.  
  115. // initialize search options
  116. if n >= 2 then
  117.   if n > 2 then
  118.     options = o
  119.   end
  120. else
  121.   options = _SearchOpt
  122. end
  123. if pos 'g' options then
  124.   options = sub 'g' '' options
  125. end
  126. options = options + '*'
  127.  
  128. // do the search
  129. resultbuf = createbuf
  130. setbuftabs (getbuftabs (getprevbuf))
  131. gotobuf (getprevbuf)
  132. pushcursor
  133. gotopos 1 1
  134. while find search_string options do
  135.   addline  getrow + ": " + gettext  '' '' resultbuf
  136.   col MAX_COL
  137. end
  138. popcursor
  139.  
  140. // check if anything was found
  141. lines = getlines resultbuf
  142. if lines <= 1 then
  143.   destroybuf resultbuf
  144.   display
  145.   say  "'" + string_and_opt + "' not found"  'b'
  146.   return
  147. end
  148.  
  149. bname = getbufname
  150.  
  151. // create find occurrences window
  152. resultwin = createwindow
  153. setframe ">bv"
  154. setcolor  border_color        findo_border_color
  155. setcolor  north_title_color   findo_title_color
  156. setcolor  text_color          findo_text_color
  157. setcolor  border_flash_color  findo_border_flash_color
  158. setcolor  menu_color          findo_menu_color
  159. setcolor  menu_hotkey_color   findo_menu_hotkey_color
  160. setcolor  menu_hilite_color   findo_menu_hilite_color
  161. settitle "Occurrences of '" + search_string + "' in " +
  162.          (onname (getname bname)) + " - " + ((getlines resultbuf) - 1) +
  163.          " lines"
  164. setwinctrl "≡" 2
  165. setborder "1i"
  166. setshadow 2 1
  167.  
  168. // create south menu bar
  169. setframe "+4"
  170. menubar '' 4
  171.   item "{Enter}=Goto+Exit"   gotoline 'x'
  172.   item "{Ctrl-G}=Goto"       gotoline
  173.   item "{Ctrl-E}=Edit"       editlines
  174.   item "{Ctrl-P}=Print"      print
  175.   item "{Esc}=Exit"          destroyobject
  176. end
  177.  
  178. // center the window
  179. width  = getvidcols - 5
  180. height = getvidrows - 10
  181. height = (if? lines >= height height lines) + 2
  182. ox = (getvidcols - width) / 2
  183. oy = (getvidrows - height) / 2
  184. sizewindow ox oy ox + width oy + height "ad"
  185.  
  186. gotobuf resultbuf
  187. delline 1 1
  188. cursor = createcursor
  189. colorcursor findo_cursor_color
  190. setwincurs cursor
  191.  
  192. // this object inherits from the 'win' object
  193. settype "win"
  194. resident ON
  195.  
  196. //--------------------------------------------------------------------
  197. // functions
  198. //--------------------------------------------------------------------
  199.  
  200. // goto the line displayed at the cursor
  201. function gotoline (gloptions)
  202.   line = gettext
  203.   if open bname then
  204.     // close occurrences window, if specified
  205.     if pos 'x' gloptions then
  206.       oldwindow = gotowindow resultwin
  207.       close
  208.       gotowindow oldwindow
  209.       resultwin = ''
  210.       destroyobject
  211.     end
  212.     gotopos 1 line [1..(pos ':' line) - 1]
  213.     send "onfound" (find search_string options + '*lg')
  214.   end
  215. end
  216.  
  217. // copy the list of occurrences to a new edit window
  218. function editlines
  219.   editbuf = createbuf
  220.   setbufname (qualify "findo.txt" bname)
  221.   gotobuf resultbuf
  222.   copyblock '*a' editbuf 1 1
  223.   gotobuf editbuf
  224.   delline
  225.   openbuf editbuf
  226. end
  227.  
  228. //--------------------------------------------------------------------
  229. // events
  230. //--------------------------------------------------------------------
  231.  
  232. variable client?
  233.  
  234. event <move>
  235.   if client? then
  236.     trackmouse
  237.   end
  238.   pass
  239. end
  240.  
  241. event <lbutton>
  242.   if getregion == 1 then
  243.     client? = TRUE
  244.     trackmouse
  245.   end
  246.   pass
  247. end
  248.  
  249. event <lbuttonup>
  250.   client? = FALSE
  251.   pass
  252. end
  253.  
  254. // double-click
  255. event <ldouble>
  256.   client? = FALSE
  257.   if getregion == 1 then
  258.     call <enter>
  259.   end
  260.   pass
  261. end
  262.  
  263. event <destroy>
  264.   if resultwin then
  265.     close
  266.   end
  267.   if resultbuf then
  268.     destroybuf resultbuf
  269.   end
  270. end
  271.  
  272. function '≡'
  273.   destroyobject
  274. end
  275.  
  276. //--------------------------------------------------------------------
  277. // keys
  278. //--------------------------------------------------------------------
  279.  
  280. // macro help
  281. macrofile = arg 1
  282. key <f1>
  283.   helpmacro macrofile
  284. end
  285.  
  286. key <up>           up
  287. key <down>         down
  288. key <left>         rollcol -1
  289. key <right>        rollcol  1
  290. key <home>         col 1
  291.  
  292. key <pgup>         pageup
  293. key <pgdn>         pagedown
  294. key <ctrl pgup>    row 1                // to list top
  295. key <ctrl pgdn>    row (getlines)       // to list bottom
  296. key <ctrl home>    row (getviewtop)     // to page top
  297. key <ctrl end>     row (getviewbot)     // to page bottom
  298.  
  299. // window functions
  300. key <f6>           nextwindow
  301. key <ctrl a>       nextwindow
  302. key <ctrl z>       maximize
  303. key <shift f3>     tile 'v'             // tile vertical
  304. key <shift f4>     tile 'h'             // tile horizontal
  305. key <shift f5>     cascade              // cascade
  306.  
  307. // south menu commands
  308. key <enter>        gotoline 'x'
  309. key <ctrl g>       gotoline
  310. key <ctrl e>       editlines
  311. key <ctrl p>       print
  312. key <ctrl p>       print
  313. key <esc>          destroyobject
  314.